package com.zx.kitchen.index.common.utils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.graphics.Bitmap;
import android.util.Log;

import com.zx.kitchen.index.common.entity.PrintOrderBean;

public class ESCUtil {

    public static final byte ESC = 27;// 换码
    public static final byte FS = 28;// 文本分隔符
    public static final byte GS = 29;// 组分隔符
    public static final byte DLE = 16;// 数据连接换码
    public static final byte EOT = 4;// 传输结束
    public static final byte ENQ = 5;// 询问字符
    public static final byte SP = 32;// 空格
    public static final byte HT = 9;// 横向列表
    public static final byte LF = 10;// 打印并换行（水平定位）
    public static final byte CR = 13;// 归位键
    public static final byte FF = 12;// 走纸控制（打印并回到标准模式（在页模式下） ）
    public static final byte CAN = 24;// 作废（页模式下取消打印数据 ）

    // ------------------------打印机初始化-----------------------------

    /**
     * 打印机初始化
     *
     * @return
     */
    public static byte[] init_printer() {
        byte[] result = new byte[2];
        result[0] = ESC;
        result[1] = 64;
        return result;
    }

    // ------------------------换行-----------------------------

    /**
     * 换行
     *
     * @param lineNum 要换几行
     * @return
     */
    public static byte[] nextLine(int lineNum) {
        byte[] result = new byte[lineNum];
        for (int i = 0; i < lineNum; i++) {
            result[i] = LF;
        }

        return result;
    }

    // ------------------------下划线-----------------------------

    /**
     * 绘制下划线（1点宽）
     *
     * @return
     */
    public static byte[] underlineWithOneDotWidthOn() {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 45;
        result[2] = 1;
        return result;
    }

    /**
     * 绘制下划线（2点宽）
     *
     * @return
     */
    public static byte[] underlineWithTwoDotWidthOn() {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 45;
        result[2] = 2;
        return result;
    }

    /**
     * 取消绘制下划线
     *
     * @return
     */
    public static byte[] underlineOff() {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 45;
        result[2] = 0;
        return result;
    }

    // ------------------------加粗-----------------------------

    /**
     * 选择加粗模式
     *
     * @return
     */
    public static byte[] boldOn() {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 69;
        result[2] = 0xF;
        return result;
    }

    /**
     * 取消加粗模式
     *
     * @return
     */
    public static byte[] boldOff() {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 69;
        result[2] = 0;
        return result;
    }

    // ------------------------对齐-----------------------------

    /**
     * 左对齐
     *
     * @return
     */
    public static byte[] alignLeft() {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 97;
        result[2] = 0;
        return result;
    }

    /**
     * 居中对齐
     *
     * @return
     */
    public static byte[] alignCenter() {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 97;
        result[2] = 1;
        return result;
    }

    /**
     * 右对齐
     *
     * @return
     */
    public static byte[] alignRight() {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 97;
        result[2] = 2;
        return result;
    }

    /**
     * 水平方向向右移动col列
     *
     * @param col
     * @return
     */
    public static byte[] set_HT_position(byte col) {
        byte[] result = new byte[4];
        result[0] = ESC;
        result[1] = 68;
        result[2] = col;
        result[3] = 0;
        return result;
    }
    // ------------------------字体变大-----------------------------

    /**
     * 字体变大为标准的n倍
     *
     * @param num
     * @return
     */
    public static byte[] fontSizeSetBig(int num) {
        byte realSize = 0;
        switch (num) {
            case 1:
                realSize = 0;
                break;
            case 2:
                realSize = 17;
                break;
            case 3:
                realSize = 34;
                break;
            case 4:
                realSize = 51;
                break;
            case 5:
                realSize = 68;
                break;
            case 6:
                realSize = 85;
                break;
            case 7:
                realSize = 102;
                break;
            case 8:
                realSize = 119;
                break;
        }
        byte[] result = new byte[3];
        result[0] = 29;
        result[1] = 33;
        result[2] = realSize;
        return result;
    }

    // ------------------------字体变小-----------------------------

    /**
     * 字体取消倍宽倍高
     *
     * @param num
     * @return
     */
    public static byte[] fontSizeSetSmall(int num) {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 33;

        return result;
    }

    // ------------------------切纸-----------------------------

    /**
     * 进纸并全部切割
     *
     * @return
     */
    public static byte[] feedPaperCutAll() {
        byte[] result = new byte[4];
        result[0] = GS;
        result[1] = 86;
        result[2] = 65;
        result[3] = 0;
        return result;
    }

    /**
     * 进纸并切割（左边留一点不切）
     *
     * @return
     */
    public static byte[] feedPaperCutPartial() {
        byte[] result = new byte[4];
        result[0] = GS;
        result[1] = 86;
        result[2] = 66;
        result[3] = 0;
        return result;
    }

    // ------------------------切纸-----------------------------
    public static byte[] byteMerger(byte[] byte_1, byte[] byte_2) {
        byte[] byte_3 = new byte[byte_1.length + byte_2.length];
        System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
        System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
        return byte_3;
    }

    public static byte[] byteMerger(byte[][] byteList) {

        int length = 0;
        for (int i = 0; i < byteList.length; i++) {
            length += byteList[i].length;
        }
        byte[] result = new byte[length];

        int index = 0;
        for (int i = 0; i < byteList.length; i++) {
            byte[] nowByte = byteList[i];
            for (int k = 0; k < byteList[i].length; k++) {
                result[index] = nowByte[k];
                index++;
            }
        }

        return result;
    }

    // --------------------
    public static byte[] generateMockData() {
        try {
            byte[] next2Line = ESCUtil.nextLine(2);
            byte[] title = "出餐单（午餐）**万通中心店".getBytes("gb2312");

            byte[] boldOn = ESCUtil.boldOn();
            byte[] fontSize2Big = ESCUtil.fontSizeSetBig(3);
            byte[] center = ESCUtil.alignCenter();
            byte[] Focus = "网 507".getBytes("gb2312");
            byte[] boldOff = ESCUtil.boldOff();
            byte[] fontSize2Small = ESCUtil.fontSizeSetSmall(3);

            byte[] left = ESCUtil.alignLeft();
            byte[] orderSerinum = "订单编号：11234".getBytes("gb2312");
            boldOn = ESCUtil.boldOn();
            byte[] fontSize1Big = ESCUtil.fontSizeSetBig(2);
            byte[] FocusOrderContent = "韭菜鸡蛋饺子-小份（单）".getBytes("gb2312");
            boldOff = ESCUtil.boldOff();
            byte[] fontSize1Small = ESCUtil.fontSizeSetSmall(2);

            next2Line = ESCUtil.nextLine(2);

            byte[] priceInfo = "应收:22元 优惠：2.5元 ".getBytes("gb2312");
            byte[] nextLine = ESCUtil.nextLine(1);

            byte[] priceShouldPay = "实收:19.5元".getBytes("gb2312");
            nextLine = ESCUtil.nextLine(1);

            byte[] takeTime = "取餐时间:2015-02-13 12:51:59".getBytes("gb2312");
            nextLine = ESCUtil.nextLine(1);
            byte[] setOrderTime = "下单时间：2015-02-13 12:35:15".getBytes("gb2312");

            byte[] tips_1 = "微信关注\"**\"自助下单每天免1元".getBytes("gb2312");
            nextLine = ESCUtil.nextLine(1);
            byte[] tips_2 = "饭后点评再奖5毛".getBytes("gb2312");
            byte[] next4Line = ESCUtil.nextLine(4);

            byte[] breakPartial = ESCUtil.feedPaperCutPartial();

            byte[][] cmdBytes = {title, nextLine, center, boldOn, fontSize2Big, Focus, boldOff, fontSize2Small,
                    next2Line, left, orderSerinum, nextLine, center, boldOn, fontSize1Big, FocusOrderContent, boldOff,
                    fontSize1Small, nextLine, left, next2Line, priceInfo, nextLine, priceShouldPay, next2Line, takeTime,
                    nextLine, setOrderTime, next2Line, center, tips_1, nextLine, center, tips_2, next4Line,
                    breakPartial};

            return ESCUtil.byteMerger(cmdBytes);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] generateMockData(PrintOrderBean printOrderBean) {

        String mTitle = printOrderBean.getSupplier().getName();
        String mMobile = printOrderBean.getSupplier().getMobile();
        String mTel = printOrderBean.getSupplier().getCompanytel();
        String mCashier = printOrderBean.getUser_name();
        String mTime = printOrderBean.getOrder().getCreate_time();
        String mCustomer = printOrderBean.getOrder().getCustomer_name();
        String MTotal = printOrderBean.getOrder().getOrder_money();
        String mOrderMoney = printOrderBean.getOrder().getOrder_money();
        String mFavourable = printOrderBean.getOrder().getDiscount_money();
        List<PrintOrderBean.OrderDetailBean> mList = printOrderBean.getOrder_detail();
        String mFavourableNum = ArithmeticUtils.sub(MTotal, mFavourable, 2);
        String mGetMoney = printOrderBean.getOrder().getReceive_money();
        String mAddress = printOrderBean.getSupplier().getCompanyaddress();

        try {
            byte[] next2Line = ESCUtil.nextLine(2);
            byte[] title = mTitle.getBytes("gb2312");
            byte[] center = ESCUtil.alignCenter();
            byte[] fontSize2Small = ESCUtil.fontSizeSetSmall(3);

            byte[] left = ESCUtil.alignLeft();
            byte[] orderMobile = ("联系电话：" + mMobile).getBytes("gb2312");
            byte[] nextLine = ESCUtil.nextLine(1);
            byte[] orderTel = ("座机电话：" + mTel).getBytes("gb2312");
            nextLine = ESCUtil.nextLine(1);
            byte[] orderCashier = ("收银员：" + mCashier).getBytes("gb2312");

            byte[] fontSize1Small = ESCUtil.fontSizeSetSmall(2);
            next2Line = ESCUtil.nextLine(2);

            byte[] takeTime = ("时间:" + mTime).getBytes("gb2312");
            nextLine = ESCUtil.nextLine(1);
            byte[] customer = ("客户名称:" + mCustomer).getBytes("gb2312");
            nextLine = ESCUtil.nextLine(1);

            byte[] listTitle = ("商品名称" + "/单位" + "/数量" + "/单价(元)" + "/金额(元)"
            ).getBytes("gb2312");

            nextLine = ESCUtil.nextLine(1);

            byte[] totalPrice = ("合计：" + MTotal).getBytes("gb2312");
            nextLine = ESCUtil.nextLine(1);

            byte[] shouldPay = ("应付金额：" + mOrderMoney + "元").getBytes("gb2312");
            nextLine = ESCUtil.nextLine(1);
            byte[] orderFavouable = ("折扣优惠：" + mFavourable + "元").getBytes("gb2312");
            nextLine = ESCUtil.nextLine(1);
            byte[] orderFavouableNum = ("优惠金额:" + mFavourableNum + "元").getBytes("gb2312");
            nextLine = ESCUtil.nextLine(1);

            byte[] priceShouldPay = ("实收:" + mGetMoney + "元").getBytes("gb2312");
            nextLine = ESCUtil.nextLine(1);


            byte[] tips_2 = ("联系地址：" + mAddress).getBytes("gb2312");
            byte[] next4Line = ESCUtil.nextLine(4);

            byte[] breakPartial = ESCUtil.feedPaperCutPartial();

            byte[][] cmdBytes = {title, nextLine, fontSize2Small,
                    next2Line, left, orderMobile, nextLine, orderTel,
                    nextLine, orderCashier, nextLine,
                    fontSize1Small, takeTime,
                    nextLine, customer, nextLine,
                    listTitle, nextLine,
            };
            byte[][] cmdBytes11 = {
                    nextLine, totalPrice, nextLine, shouldPay, nextLine, orderFavouable,
                    nextLine, orderFavouableNum, nextLine, priceShouldPay, next2Line, center, tips_2, next4Line,
                    breakPartial};
            byte[][] cmdBytes1 = new byte[mList.size()*2][];
            for (int i = 0; i < mList.size()*2; i++) {
                cmdBytes1[i] = nextLine;
            }
            for (int i = 0; i < mList.size(); i++) {
                byte[] list1 = (mList.get(i).getName() + "/" + mList.get(i).getUnit() + "/" + mList.get(i).getNum() + "/" +
                            mList.get(i).getPrice() + "/" + mList.get(i).getPrice()
                    ).getBytes("gb2312");
                    cmdBytes1[i*2] = list1;
            }
            byte[][] cmdBytes2 = new byte[cmdBytes.length + cmdBytes1.length + cmdBytes11.length][];
            for (int i = 0; i < cmdBytes2.length; i++) {
                if (i >= cmdBytes.length + cmdBytes1.length) {
                    cmdBytes2[i] = cmdBytes11[i - cmdBytes.length - cmdBytes1.length];
                } else if (i >= cmdBytes.length) {
                    cmdBytes2[i] = cmdBytes1[i - cmdBytes.length];
                } else {
                    cmdBytes2[i] = cmdBytes[i];
                }
            }

            return ESCUtil.byteMerger(cmdBytes2);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

}